package xuanzhen.manage.controller;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import xuanzhen.dating.service.SeeShareService;
import xuanzhen.manage.pojo.Admins;
import xuanzhen.manage.service.AdminsService;
import xuanzhen.reservation.pojo.BlackUser;
import xuanzhen.reservation.pojo.PostMan;
import xuanzhen.reservation.pojo.Reservation;
import xuanzhen.reservation.service.BlackUserService;
import xuanzhen.reservation.service.PostManService;
import xuanzhen.reservation.service.ReservationService;

@Controller
@RequestMapping("/reserManage")
public class ReserManegeController {
	@Resource
	private AdminsService adminsService;
	@Resource
	private SeeShareService seeshareService;
	@Resource
	private PostManService postmanService;
	@Resource
	private ReservationService reservationService;
	@Resource
	private BlackUserService blackUserService;
	/**
	 * 约车调度页面
	 */
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String state=request.getParameter("orderState");
		if(state!=null&&state.equals("0")){
			state=null;
		}
		Map<String,Object> parameter = new HashMap<String,Object>();
		String keywords = request.getParameter("keywords");
		if(keywords != null && !"".equals(keywords.trim())){
			keywords = new String(keywords.trim().getBytes("ISO-8859-1"), "UTF-8");
			model.addAttribute("keys",keywords);
			parameter.put("name", keywords);
			parameter.put("phone", keywords);
			parameter.put("area", keywords);
			parameter.put("address", keywords);
			parameter.put("reRemark", keywords);
			parameter.put("pName", keywords);
		}
		parameter.put("pageBegin", 0);
		parameter.put("pageSize", 10);
		parameter.put("state", state);
		model.addAttribute("orderState", state);
		List<Reservation> resList =  reservationService.queryLikeListByMap(parameter);
		int counts = reservationService.countLikes(parameter);
		model.addAttribute("countSize",counts);
		model.addAttribute("resList", resList);

		//总页数
		int page = 0;
		if(counts>10){
			if(counts%10==0){
				 page = counts/10;
			}else{
				 page= counts/10+1;
			}
		}else{
			page=1;
		}
		
		model.addAttribute("pageCount",page);
		return "manage/reservationList";
		
	}
	/**
	 * 约车调度页面条件搜索翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object seer(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("pageNo")int pagebegin,@Param("orderState")String orderState,@Param("keywords")String keywords)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		if(orderState!=null&&orderState.equals("0")){
			orderState=null;
		}
		if(keywords!=null && !"".equals(keywords.trim())){
			model.addAttribute("keys",keywords);
			parameter.put("name", keywords);
			parameter.put("phone", keywords);
			parameter.put("area", keywords);
			parameter.put("address", keywords);
			parameter.put("reRemark", keywords);
			parameter.put("pName", keywords);
		}
		parameter.put("state", orderState);
		parameter.put("pageBegin", pagebegin*10);
		parameter.put("pageSize", 10);
		List<Reservation> resList =  reservationService.queryLikeListByMap(parameter);
		return resList;
		
	}
	/**
	 * 约车调度页面未读消息数
	 */
	@RequestMapping(value = "/unread", method = RequestMethod.POST)
	@ResponseBody
	public int unread(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("state", "RECEIVE");
		int resList =  reservationService.countLikes(parameter);
			return resList;
		
	}
	/**
	 * 约车调度页面
	 */
	@RequestMapping(value = "/post", method = RequestMethod.POST)
	public String paifa(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("rid")int rid,@Param("pid")int pid,@Param("pname")String pname,@Param("remark")String remark)
			throws Exception {
		Reservation reser = new Reservation();
		reser.setId(rid);
		reser.setpId(pid);
		reser.setpName(pname);
		reser.setState("UNFINISH");
		reser.setRemark(remark);
		//createTime
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		ParsePosition pos = new ParsePosition(0);
		Date currentTime_2 = dateFormat.parse(nowTime, pos);
		reser.setDistributeTime(currentTime_2);
		 reservationService.updateReservation(reser);
		  //修改接单数
		 Map<String, Object> condition = new HashMap<String , Object>();
		 condition.put("pId", pid);
		 int counts = reservationService.countUnDeul(condition);
		 PostMan postman = new PostMan();
		 postman.setId(pid);
		 postman.setAcceptCount(counts);
		 postmanService.updatePostMan(postman);
			return "login";
		
	}
	/**
	 * 拒绝约车
	 */
	@RequestMapping(value = "/dis", method = RequestMethod.POST)
	public void back(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("rid")int rid)
			throws Exception {
		Reservation reser = new Reservation();
		reser.setId(rid);
		reser.setState("REFUSE");
		//createTime
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
				String nowTime = dateFormat.format(now);
				ParsePosition pos = new ParsePosition(0);
				Date currentTime_2 = dateFormat.parse(nowTime, pos);
				reser.setDistributeTime(currentTime_2);
				 reservationService.updateReservation(reser);
		
	}
	/**
	 * 操作检查状态
	 */
	@RequestMapping(value = "/checkstate", method = RequestMethod.POST)
	@ResponseBody
	public String state(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("rid")int rid)
			throws Exception {
		Reservation reser = new Reservation();
		reser.setId(rid);
		Reservation rese =reservationService.queryListByEntity(reser).get(0);
		return rese.getState();
	}
	/**
	 * 约车消息处理查询
	 */
	@RequestMapping(value = "/unreaddeal", method = RequestMethod.POST)
	@ResponseBody
	public Object deal(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("pagebegin")int pagebegin)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("state", "RECEIVE");
		parameter.put("pageBegin", pagebegin*10);
		parameter.put("pageSize", 10);
		List<Reservation> resList =  reservationService.queryListByMap(parameter);
		//总页数		
			return resList;
		
	}
	
	/**
	 * 配送员查询
	 */
	@RequestMapping(value = "/chick", method = RequestMethod.GET)
	@ResponseBody
	public Object chick(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		List<PostMan> pList = postmanService.queryListByMap(parameter);
			return pList;
		
	}
	/**
	 * 配送员类表页面
	 */
	@RequestMapping(value = "/postman", method = RequestMethod.GET)
	public String postman(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		List<PostMan> resList =  postmanService.selectpostListByMap(parameter);
		model.addAttribute("postManList", resList);
			return "manage/postManList";
		
	}
	/**
	 * 配送员类表页面
	 */
	@RequestMapping(value = "/postManTask", method = RequestMethod.POST)
	@ResponseBody
	public Object postManTask(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("id")int id)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("pId", id);
		parameter.put("state", "UNFINISH");
		List<Reservation> resList =  reservationService.queryListByMap(parameter);
		
			return resList;
	}
	/**
	 * 黑名单候选页面
	 */
	@RequestMapping(value = "/blackUserList", method = RequestMethod.GET)
	public Object blackUserList(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		List<BlackUser> userList =  blackUserService.queryListByMap(parameter);
		model.addAttribute("blackUserList", userList);
		List<BlackUser> resList =  blackUserService.selectUserNotBlack(parameter);
		model.addAttribute("blackNotUserList", resList);
			return "manage/blackUserList";
	}
	/**
	 * 配送员类表页面
	 */
	@RequestMapping(value = "/insertBlack", method = RequestMethod.POST)
	@ResponseBody
	public Object insertblack(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("phone")String phone)
			throws Exception {
		BlackUser blackUser = new BlackUser();
		blackUser.setBlackPhone(phone);
		blackUser.setBlackDeleteState(1);
		blackUser.setBlackBeginTime(new Date());
		 Date da = blackUser.getBlackBeginTime();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(da);
			calendar.add(Calendar.YEAR,100 );
			blackUser.setBlackEndTime(calendar.getTime());
			int w = blackUserService.insertBlackUser(blackUser);
			return w;
	}
	/**
	 * 配送员类表页面
	 */
	@RequestMapping(value = "/outBlack", method = RequestMethod.POST)
	@ResponseBody
	public Object outBlack(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@Param("id")int id)
			throws Exception {
		
			return blackUserService.deleteByPrimaryKey(id);
	}
	
}
